package com.company.project.controller;

import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.ReportEquipment;
import com.company.project.model.dto.getAllReportEquipmentDto;
import com.company.project.model.dto.getkhdwDto;
import com.company.project.service.ReportEquipmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Calendar;
import java.util.List;

/**
 * Created by DLHTX on 2020/06/17.
 */
@Api("Swagger示例CRUD操作")
@RestController
@RequestMapping("/api/reportEquipment")

public class ReportEquipmentController {
    @Resource
    private ReportEquipmentService reportEquipmentService;

    @ApiOperation("获取所有检修报告")
    @PostMapping("/getAllReportEquipment")
    public Result list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer row, @RequestBody() getAllReportEquipmentDto param) {
        PageHelper.startPage(page, row);
        List<ReportEquipment> list = reportEquipmentService.getAllReportEquipment(param);
        System.out.println(list);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @ApiOperation("获取客户单位统计图")
    @GetMapping("/getKhdwPie")
    public Result getKhdwPie() {
        List<getkhdwDto> list = reportEquipmentService.getKhdwPie();
        return ResultGenerator.genSuccessResult(list);
    }

    @ApiOperation("获取销售人员统计图")
    @GetMapping("/getsalesmanPie")
    public Result getsalesmanPie() {
        List<getkhdwDto> list = reportEquipmentService.getsalesmanPie();
        return ResultGenerator.genSuccessResult(list);
    }

    @ApiOperation("获取驾驶员出勤统计图")
    @GetMapping("/getbackdriverPie")
    public Result getbackdriverPie() {
        List<getkhdwDto> list = reportEquipmentService.getbackdriverPie();
        return ResultGenerator.genSuccessResult(list);
    }

    @ApiOperation("获取设备类别")
    @GetMapping("/getsbxhPie")
    public Result getsbxhPie() {
        List<getkhdwDto> list = reportEquipmentService.getsbxhPie();
        return ResultGenerator.genSuccessResult(list);
    }

    @ApiOperation("更新检修报告")
    @PostMapping("/updateReportEquipment")
    public Result updateReportEquipment(@RequestBody() ReportEquipment param) throws IllegalAccessException {
        String sql = "";
        //Field[ ]  fields = param.class.getDeclaredFields( ) ;    取得实体类的所有属性集合。
        //String attrName = fields[0].getName()  ;获得属性集合中第一个属性的名字
        //Object  attrValue = fields[0].get(qlfctBasInfo) ; 获得属性集合中第一个属性的值
        //Field field = QlfctBasInfo.class.getDeclaredField(“name” ) ;   取得实体类中"name"字段的Field对象
        //field.setAccessible(true); //,如果字段是私有的,那么必须要对这个字段设置,这样才可以正常使用.否则或报错:can not access a member of.......
        //sql.substring(0,sql.length() -1) 去除最后一个逗号
        Field[] paramArrayList = param.getClass().getDeclaredFields();
        for (int i = 0; i < paramArrayList.length; i++) {
            paramArrayList[i].setAccessible(true);
            System.out.println(paramArrayList[i].getName() + ":" + paramArrayList[i].get(param));
            if (paramArrayList[i].get(param) == "true") {
                paramArrayList[i].set(param, "√");
            }
            if (paramArrayList[i].get(param) == "false") {
                paramArrayList[i].set(param, " ");
            }
            if (paramArrayList[i].getName() != "id" && paramArrayList[i].getName() != "isDelete" && paramArrayList[i].get(param) != null && paramArrayList[i].get(param) != "null") {
                sql += (paramArrayList[i].getName() + "='" + paramArrayList[i].get(param) + "',");
            }
        }
        String BaseSql = "UPDATE report_equipment SET " + sql.substring(0, sql.length() - 1) + " WHERE id =" + param.getId();
        int resTrueFalse = reportEquipmentService.updateReportEquipment(BaseSql);
        return ResultGenerator.genSuccessResult(resTrueFalse);
    }


    @ApiOperation("更新检修报告")
    @PostMapping("/addReportEquipment")
    public Result addReportEquipment(@RequestBody() ReportEquipment param) throws IllegalAccessException {
        long newWork_id;//work_id需要我生成
        String sql1 = "";
        String sql2 = "";
        //String string = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).toString();
        //System.out.println(string);//2019-06-17

        Calendar calendar = Calendar.getInstance();
        if (calendar.get(Calendar.MONTH) + 1 == 1 & calendar.get(Calendar.DATE) == 1) {
            newWork_id = 1;
        } else {
            List<ReportEquipment> work_idList = reportEquipmentService.getWork_id();
            long firstWork_id = Long.parseLong(work_idList.get(0).getWork_id());
            newWork_id = firstWork_id + 1;
        }
        param.setWork_id(Long.toString(newWork_id));

        Field[] paramArrayListAdd = param.getClass().getDeclaredFields();
        for (int i = 0; i < paramArrayListAdd.length; i++) {
            paramArrayListAdd[i].setAccessible(true);
            System.out.println(paramArrayListAdd[i].getName() + ":" + paramArrayListAdd[i].get(param));
            if (paramArrayListAdd[i].get(param) == "true") {
                paramArrayListAdd[i].set(param, "√");
            }
            if (paramArrayListAdd[i].get(param) == "false") {
                paramArrayListAdd[i].set(param, " ");
            }
        }

        int resTrueFalse = reportEquipmentService.addReportEquipment(param);
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation("获取isDelete为0的")
    @PostMapping("/getAllReportEquipmentNoWhere")
    public Result getAllReportEquipmentNoWhere() {
        List<ReportEquipment> list = reportEquipmentService.getAllReportEquipmentNoWhere();
        return ResultGenerator.genSuccessResult(list);
    }


    @ApiOperation("获取isDelete为0的")
    @PostMapping("/deleteEquipmentByid")
    public Result deleteEquipmentByid(@RequestBody() ReportEquipment param) {
        int res = reportEquipmentService.deleteEquipmentByid(param);
        return ResultGenerator.genSuccessResult(res);
    }

    @ApiOperation("调度驾驶员")
    @PostMapping("/updateDispatch")
    public Result updateDispatch(@RequestBody() ReportEquipment param) {
        int res = reportEquipmentService.updateDispatch(param);
        return ResultGenerator.genSuccessResult(res);
    }

    @ApiOperation("更新预算")
    @PostMapping("/updateBudget")
    public Result updateBudget(@RequestBody() ReportEquipment param) {
        int res = reportEquipmentService.updateBudget(param);
        return ResultGenerator.genSuccessResult(res);
    }

    @ApiOperation("更新结算")
    @PostMapping("/updateAccount")
    public Result updateAccount(@RequestBody() ReportEquipment param) {
        int res = reportEquipmentService.updateAccount(param);
        return ResultGenerator.genSuccessResult(res);
    }























    //全部查询接口
//    @UserLoginToken
    @ApiOperation("查询车间")
    @GetMapping("/findWorkShop")
    public Result findWorkShop(@RequestParam() String search) {
        String baseSql = "select distinct workshop from report_equipment where workshop LIKE '%" + search + "%' and not isnull(workshop)";
        List<ReportEquipment> list = reportEquipmentService.searchCommonFunction(baseSql);
        return ResultGenerator.genSuccessResult(list);
    }

    @ApiOperation("查询客户单位")
    @GetMapping("/findKhdw")
    public Result findKhdw(@RequestParam() String search) {
        String baseSql = "select distinct khdw from report_equipment where khdw LIKE '%" + search + "%' and not isnull(khdw)";
        List<ReportEquipment> list = reportEquipmentService.searchCommonFunction(baseSql);
        return ResultGenerator.genSuccessResult(list);
    }

    @ApiOperation("查询装置名称")
    @GetMapping("/findDeviceName")
    public Result findDeviceName(@RequestParam() String search) {
        String baseSql = "select distinct device_name from report_equipment where device_name LIKE '%" + search + "%' and not isnull(device_name)";
        List<ReportEquipment> list = reportEquipmentService.searchCommonFunction(baseSql);
        return ResultGenerator.genSuccessResult(list);
    }

    @ApiOperation("查询甲方负责人")
    @GetMapping("/findPartA")
    public Result findPartA(@RequestParam() String search) {
        String baseSql = "select distinct part_a from report_equipment where part_a LIKE '%" + search + "%' and not isnull(part_a)";
        List<ReportEquipment> list = reportEquipmentService.searchCommonFunction(baseSql);
        return ResultGenerator.genSuccessResult(list);
    }

    @ApiOperation("查询轴承型号font")
    @GetMapping("/findzcxhf")
    public Result findzcxhf(@RequestParam() String search) {
        String baseSql = "select distinct zcxh_font from report_equipment where zcxh_font LIKE '%" + search + "%' and not isnull(zcxh_font)";
        List<ReportEquipment> list = reportEquipmentService.searchCommonFunction(baseSql);
        return ResultGenerator.genSuccessResult(list);
    }

    @ApiOperation("查询轴承型号back")
    @GetMapping("/findzcxhb")
    public Result findzcxhb(@RequestParam() String search) {
        String baseSql = "select distinct zcxh_back from report_equipment where zcxh_back LIKE '%" + search + "%' and not isnull(zcxh_back)";
        List<ReportEquipment> list = reportEquipmentService.searchCommonFunction(baseSql);
        return ResultGenerator.genSuccessResult(list);
    }


    @ApiOperation("查询司机")
    @GetMapping("/findbackDriver")
    public Result findbackDriver(@RequestParam() String search) {
        String baseSql = "select distinct back_driver from report_equipment where back_driver LIKE '%" + search + "%' and not isnull(back_driver)";
        List<ReportEquipment> list = reportEquipmentService.searchCommonFunction(baseSql);
        return ResultGenerator.genSuccessResult(list);
    }

    @ApiOperation("查询司机")
    @GetMapping("/findgoDriver")
    public Result findgoDriver(@RequestParam() String search) {
        String baseSql = "select distinct go_driver from report_equipment where go_driver LIKE '%" + search + "%' and not isnull(go_driver)";
        List<ReportEquipment> list = reportEquipmentService.searchCommonFunction(baseSql);
        return ResultGenerator.genSuccessResult(list);
    }

    @ApiOperation("findkhjsr")
    @GetMapping("/findkhjsr")
    public Result findkhjsr(@RequestParam() String search) {
        String baseSql = "select distinct khjsr from report_equipment where khjsr LIKE '%" + search + "%' and not isnull(khjsr)";
        List<ReportEquipment> list = reportEquipmentService.searchCommonFunction(baseSql);
        return ResultGenerator.genSuccessResult(list);
    }

    @ApiOperation("findsalesMan")
    @GetMapping("/findsalesMan")
    public Result findsalesMan(@RequestParam() String search) {
        String baseSql = "select distinct salesman from report_equipment where salesman LIKE '%" + search + "%' and not isnull(salesman)";
        List<ReportEquipment> list = reportEquipmentService.searchCommonFunction(baseSql);
        return ResultGenerator.genSuccessResult(list);
    }
    //全部查询接口


    @Controller
    public static class IndexController {
        @RequestMapping("/")
        public void  index(HttpServletResponse response) throws IOException {
            response.sendRedirect("/index.html");
        }
    }
}
